import router from './router'
import store from './store'
router.beforeEach(async (to, from, next) => {
  if ((store.state.stores.length === 0 || store.state.region.length === 0)) {
    await Promise.all([store.dispatch('GetStores'), store.dispatch('GetRegion')])
  }
  if (!store.state.info) {
    let res = await store.dispatch('GetMe')
    console.log(res)
    if (res) {
      next(to.path)
    } else {
      to.path === '/login' ? next() : next('/login');
    }
  } else if (to.path === '/list') {
    await store.dispatch('GetList')
    store.commit('SET_ANSWER', false)
    next();
  } else if (store.state.hasAnswer || store.state.quesList.length === 0 || Object.keys(store.state.ques).length === 0) {
    next('/list');
  } else {
    next();
  }
})